/**
 * 
 */
package com.yjd.bi.statistic.control;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.yjd.bi.borrow.service.IProfitFormService;
import com.yjd.bi.business.service.IFundsDataService;
import com.yjd.bi.rewards.service.IActReturnAwardDataService;
import com.yjd.bi.rewards.service.IActReturnAwardService;
import com.yjd.bi.rewards.service.IFundsFinancialDataService;
import com.yjd.bi.rewards.service.IFundsFinancialService;
import com.yjd.bi.rewards.service.IFundsVolumeDataService;
import com.yjd.bi.rewards.service.IFundsVolumeService;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import com.yjd.comm.util.ServletUtil;

/**
 * @author Administrator
 * 
 */
@Controller
@Scope("prototype")
@RequestMapping("rewards")
public class RewardsControl extends BaseControl {

	/**
	 * 券類獎勵
	 * 
	 * @param starttime
	 * @param endtime
	 * @throws Exception
	 */
	@RequestMapping("voucher")
	public String voucher(@RequestParam(required = false) String starttime,
			@RequestParam(required = false) String endtime) throws Exception {
		Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
		paramMap.put("currentDate",
				DateUtil.format(new Date(), DateUtil.PATTERN_yyyy_MM_dd));
		if (StringUtils.isBlank(endtime)) {
			endtime = DateUtil.format(DateUtils.addDays(new Date(), -1),
					DateUtil.PATTERN_yyyy_MM_dd);
		}
		if (StringUtils.isBlank(starttime)) {
			starttime = DateUtil.format(DateUtils.addDays(
					DateUtil.parseDate(endtime, DateUtil.PATTERN_yyyy_MM_dd),
					-7), DateUtil.PATTERN_yyyy_MM_dd);
		}
		paramMap.put("starttime", starttime);
		paramMap.put("endtime", endtime);
		paramMap.put("start_time", DateUtil.dateStringToTimestamp(starttime,
				DateUtil.PATTERN_yyyy_MM_dd));
		paramMap.put("end_time", FrameUtil.getTime(DateUtils.addSeconds(
				DateUtils.addDays(DateUtils.truncate(DateUtil.parseDate(
						endtime, DateUtil.PATTERN_yyyy_MM_dd),
						Calendar.DAY_OF_MONTH), 1), -1)));
		request.setAttribute("paramMap", paramMap);

		double volumeTotalAmount = fundsVolumeService.getTotalAmount(paramMap);
		double volumeDataTotalAmount = fundsVolumeDataService
				.getTotalAmount(paramMap);
		double raiseTotalAmount = profitFormService
				.getTotalRaiseAmount(paramMap);
		double financialTotalAmount = ArithUtil.add(
				fundsFinancialService.getTotalAmount(paramMap),
				actReturnAwardService.getTotalAmount(paramMap));
		double financialDataTotalAmount = ArithUtil.add(
				fundsFinancialDataService.getTotalReturnAmount(paramMap),
				actReturnAwardDataService.getTotalAmount(paramMap));
		double totalAmount = ArithUtil.adds(volumeDataTotalAmount,
				raiseTotalAmount, financialDataTotalAmount);
		request.setAttribute("resultMap", FrameUtil.newHashMap(
				"volumeTotalAmount",
				NumberUtil.formatNumber4(volumeTotalAmount),
				"volumeDataTotalAmount",
				NumberUtil.formatNumber4(volumeDataTotalAmount),
				"totalRaiseAmount", NumberUtil.formatNumber4(raiseTotalAmount),
				"financialTotalAmount",
				NumberUtil.formatNumber4(financialTotalAmount),
				"totalReturnAmount",
				NumberUtil.formatNumber4(financialDataTotalAmount),
				"totalAmount", NumberUtil.formatNumber4(totalAmount)));
		return "rewards.voucher";
	}

	/**
	 * 現金獎勵
	 * 
	 * @param starttime
	 * @param endtime
	 * @throws Exception
	 */
	@RequestMapping("money")
	public String money(@RequestParam(required = false) String starttime,
			@RequestParam(required = false) String endtime) throws Exception {
		Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
		paramMap.put("currentDate",
				DateUtil.format(new Date(), DateUtil.PATTERN_yyyy_MM_dd));
		if (StringUtils.isBlank(endtime)) {
			endtime = DateUtil.format(DateUtils.addDays(new Date(), -1),
					DateUtil.PATTERN_yyyy_MM_dd);
		}
		if (StringUtils.isBlank(starttime)) {
			starttime = DateUtil.format(DateUtils.addDays(
					DateUtil.parseDate(endtime, DateUtil.PATTERN_yyyy_MM_dd),
					-7), DateUtil.PATTERN_yyyy_MM_dd);
		}
		paramMap.put("starttime", starttime);
		paramMap.put("endtime", endtime);
		paramMap.put("moreType", 14);
		paramMap.put("start_time", DateUtil.dateStringToTimestamp(starttime,
				DateUtil.PATTERN_yyyy_MM_dd));
		paramMap.put("end_time", FrameUtil.getTime(DateUtils.addSeconds(
				DateUtils.addDays(DateUtils.truncate(DateUtil.parseDate(
						endtime, DateUtil.PATTERN_yyyy_MM_dd),
						Calendar.DAY_OF_MONTH), 1), -1)));
		request.setAttribute("paramMap", paramMap);
		request.setAttribute("resultMap", FrameUtil.newHashMap("totalCount",
				fundsDataService.getTotalCount(paramMap), "totalAmount",
				NumberUtil.formatNumber4(fundsDataService
						.getTotalAmount(paramMap))));
		return "rewards.money";
	}

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IFundsVolumeService fundsVolumeService;

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	public IFundsVolumeDataService fundsVolumeDataService;

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	public IProfitFormService profitFormService;

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	public IFundsFinancialService fundsFinancialService;

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	public IFundsFinancialDataService fundsFinancialDataService;

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	public IActReturnAwardService actReturnAwardService;

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	public IActReturnAwardDataService actReturnAwardDataService;

	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	public IFundsDataService fundsDataService;

}
